{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In this example we will take a small set of colors in the RGB space and use the intensity values to train a SOM. After the som is trained we will plot the map coloring each neuron using its weights as RGB coordinates.\n",
    "\n",
    "Let's first define our colors:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from minisom import MiniSom\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "%load_ext autoreload\n",
    "\n",
    "#Training inputs for RGBcolors\n",
    "colors = [[0., 0., 0.],\n",
    "      [0., 0., 1.],\n",
    "      [0., 0., 0.5],\n",
    "      [0.125, 0.529, 1.0],\n",
    "      [0.33, 0.4, 0.67],\n",
    "      [0.6, 0.5, 1.0],\n",
    "      [0., 1., 0.],\n",
    "      [1., 0., 0.],\n",
    "      [0., 1., 1.],\n",
    "      [1., 0., 1.],\n",
    "      [1., 1., 0.],\n",
    "      [1., 1., 1.],\n",
    "      [.33, .33, .33],\n",
    "      [.5, .5, .5],\n",
    "      [.66, .66, .66]]\n",
    "color_names = \\\n",
    "    ['black', 'blue', 'darkblue', 'skyblue',\n",
    "     'greyblue', 'lilac', 'green', 'red',\n",
    "     'cyan', 'violet', 'yellow', 'white',\n",
    "     'darkgrey', 'mediumgrey', 'lightgrey']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's initialize our SOM and plot the colors without training:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "som = MiniSom(30, 30, 3, sigma=3., \n",
    "              learning_rate=2.5, \n",
    "              neighborhood_function='gaussian')\n",
    "\n",
    "plt.imshow(abs(som.get_weights()), interpolation='none')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now train the SOM and check how the organization of the weights has changed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "som.train(colors, 500, random_order=True, verbose=True)\n",
    "\n",
    "plt.imshow(abs(som.get_weights()), interpolation='none')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using a different neighborhood function the weights be less smooth across the map:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "som = MiniSom(30, 30, 3, sigma=8., \n",
    "              learning_rate=.5, \n",
    "              neighborhood_function='bubble')\n",
    "som.train_random(colors, 500, verbose=True)\n",
    "\n",
    "plt.imshow(abs(som.get_weights()), interpolation='none')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
